/*
 * Copyright (C) 2009 CityLights Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.schizoidman.android.citylights.model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.net.Uri;

public class TCProjectProxy extends TCEntityLoader {
	
//    static private Map<Uri, TCServer> INSTANCES = new HashMap<Uri, TCServer>();
	public static final String NAME = "project.name";
	public static final String ID = "project.id";
	public static final String HREF = "project.href";
	public static final String FAILED_BUILDS_COUNT = "project.failures.count";
    
	private List<TCBuildTypeProxy> mBuildTypes = new ArrayList<TCBuildTypeProxy> ();

	public TCProjectProxy(Intent intent) {
		super (intent);
		getData().setClassName("com.schizoidman.android.citylights", "com.schizoidman.android.citylights.BuildConfigsListPage");
	}

	public TCProjectProxy (Intent sender, JSONObject jsObject) {
		this (sender);
		init(jsObject);
	}
	
	private void init (JSONObject jsObject) {
		Intent intent = getData();
		intent.putExtra(NAME, jsObject.optString("@name"));
		intent.putExtra(ID, jsObject.optString("@id"));
		intent.putExtra(HREF, jsObject.optString("@href"));
		// updating Intent URI
		intent.setData(Uri.parse("http://" + intent.getData().getAuthority().concat(intent.getStringExtra(HREF))));
	}
		
	@Override
	protected void loadFromJSON(String jsonString) throws JSONException
	{
		JSONObject jsProject=new JSONObject(jsonString);

		JSONObject jsBuildTypesRoot=jsProject.getJSONObject("buildTypes");
		
		// ---  Workaround: When there's just one element, JSON returns one object instead of array
		JSONObject jsBuildType = jsBuildTypesRoot.optJSONObject("buildType");
		if (null != jsBuildType) {
			mBuildTypes.add (new TCBuildTypeProxy(getData(), jsBuildType));
		}
		// ---
		
		JSONArray jsBuildTypes=jsBuildTypesRoot.optJSONArray("buildType");
		if (null != jsBuildTypes) {
			for(int i=0; i<jsBuildTypes.length(); i++)
			{
				mBuildTypes.add (new TCBuildTypeProxy(getData(), jsBuildTypes.getJSONObject(i)));
			}
		}
	}
	
	public void loadWithBuildStatuses() throws JSONException, IOException, ClientProtocolException {
		this.load();
		int failuresCount = 0;

   		for (Iterator<TCBuildTypeProxy> iterator = mBuildTypes.iterator(); iterator.hasNext();) {
   			TCBuildTypeProxy buildType = iterator.next();
   			buildType.load();
   			failuresCount += buildType.getLastBuildStatus() == TCBuildProxy.BUILD_FAILURE ? 1:0;
		}
   		getData().putExtra(FAILED_BUILDS_COUNT, failuresCount);
	}
	
	public List<TCBuildTypeProxy> getBuildTypes() {
		return mBuildTypes;
	}

	@Override
	public String toString() {
		return getData().getStringExtra(NAME);
	}

}